﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VatTuDTO;
using MySql.Data.MySqlClient;
using System.Data;
using VatTuDAO;


namespace VatTuDAO1
{
    public class HoaDonDAO : BaseDataProvider
    {
        MySqlCommand _cmd;


        //Lấy danh sách hoa don 
        public List<HoaDonDTO> SelectHoaDon()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from HoaDon";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            List<HoaDonDTO> ds = new List<HoaDonDTO>();
            HoaDonDTO dto;

            try
            {

                foreach (DataRow row in dt.Rows)
                {
                    dto = new HoaDonDTO();
                    dto.MaHoaDon = int.Parse(row["MaHoaDon"].ToString());
                    dto.MaNguoiDung = int.Parse(row["MaNguoiDung"].ToString());
                    dto.NgayGioDatHang = DateTime.Parse(row["NgayGioDatHang"].ToString());
                    dto.NgayGioGiaoHang = DateTime.Parse(row["NgayGioGiaoHang"].ToString());
                    dto.DiaDiemGiaoHang = row["DiaDiemGiaoHang"].ToString();
                    dto.TenNguoiNhan = row["TenNguoiNhan"].ToString();
                    dto.TongTien = int.Parse(row["TongTien"].ToString());
                    dto.HinhThucTHanhToan = row["HinhThucThanhToan"].ToString();
                    dto.MaTinhTrang =int.Parse( row["MaTinhTrang"].ToString());

                                     
                    ds.Add(dto);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ds;
        }


        // Lấy 1 hoa don co ma hoa don 
        public HoaDonDTO SelectHoaDon(int maHoaDon)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from HoaDon where MaHoaDon=@MaHoaDon";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaHoaDon", maHoaDon));
            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            HoaDonDTO dto = new HoaDonDTO();

            try
            {

                DataRow row = dt.Rows[0];//kết quả trả ra ở dòng 0

                dto = new HoaDonDTO();
                dto.MaHoaDon = int.Parse(row["MaHoaDon"].ToString());
                dto.MaNguoiDung = int.Parse(row["MaNguoiDung"].ToString());
                dto.NgayGioDatHang = DateTime.Parse(row["NgayGioDatHang"].ToString());
                dto.NgayGioGiaoHang = DateTime.Parse(row["NgayGioGiaoHang"].ToString());
                dto.DiaDiemGiaoHang = row["DiaDiemGiaoHang"].ToString();
                dto.TenNguoiNhan = row["TenNguoiNhan"].ToString();
                dto.TongTien = int.Parse(row["TongTien"].ToString());
                dto.HinhThucTHanhToan = row["HinhThucThanhToan"].ToString();
                dto.MaTinhTrang = int.Parse(row["MaTinhTrang"].ToString());


               
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dto;
        }


        public Boolean InsertHoaDon(HoaDonDTO HoaDon)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into HoaDon(MaNguoiDung,NgayGioDatHang,NgayGioGiaoHang,DiaDiemGiaoHang,TenNguoiNhan,TongTien,HinhThucThanhToan,MaTinhTrang)";
            _cmd.CommandText +="values(@MaNguoiDung,@NgayGioDatHang,@NgayGioGiaoHang,@DiaDiemGiaoHang,@TenNguoiNhan,@TongTien,@HinhThucThanhToan,@MaTinhTrang)";
            _cmd.CommandType = CommandType.Text;

            _cmd.Parameters.Add(new MySqlParameter("@MaNguoiDung", HoaDon.MaNguoiDung));
            _cmd.Parameters.Add(new MySqlParameter("@NgayGioDatHang", HoaDon.NgayGioDatHang));
            _cmd.Parameters.Add(new MySqlParameter("@NgayGioGiaoHang", HoaDon.NgayGioGiaoHang));
            _cmd.Parameters.Add(new MySqlParameter("@DiaDiemGiaoHang", HoaDon.DiaDiemGiaoHang));
            _cmd.Parameters.Add(new MySqlParameter("@TenNguoiNhan", HoaDon.TenNguoiNhan));
           
            _cmd.Parameters.Add(new MySqlParameter("@TongTien", HoaDon.TongTien));
            _cmd.Parameters.Add(new MySqlParameter("@HinhThucThanhToan", HoaDon.HinhThucTHanhToan));
            _cmd.Parameters.Add(new MySqlParameter("@MaTinhTrang", HoaDon.MaTinhTrang));
         

            return ExecuteNonQuery(_cmd);
        }

        // update hoa don voi mahoadon va tinhtrang

        public Boolean UpdateHoaDon(int MaHoaDon, int MaTinhTrang)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update HoaDon set MaTinhTrang = @MaTinhTrang where MaHoaDon=@MaHoaDon";
            _cmd.CommandType = CommandType.Text;

            _cmd.Parameters.Add(new MySqlParameter("@MaHoaDon", MaHoaDon.ToString()));
            _cmd.Parameters.Add(new MySqlParameter("@MaTinhTrang", MaTinhTrang.ToString()));


            return ExecuteNonQuery(_cmd);
        }
        public Boolean UpdateHoaDonHinhThucThanhToan(int MaHoaDon, string hinhThucThanhToan)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update HoaDon set HinhThucThanhToan = @HinhThucThanhTOan where MaHoaDon=@MaHoaDon";
            _cmd.CommandType = CommandType.Text;

            _cmd.Parameters.Add(new MySqlParameter("@MaHoaDon", MaHoaDon.ToString()));
            _cmd.Parameters.Add(new MySqlParameter("@HinhThucThanhToan", hinhThucThanhToan));


            return ExecuteNonQuery(_cmd);
        }
        // delete hoa don voi mahoadon
        public Boolean DeleteHoaDon(int MaHoaDon)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from HoaDon where MaHoaDon = @MaHoaDon";
            _cmd.CommandType = CommandType.Text;

            _cmd.Parameters.Add(new MySqlParameter("@MaHoaDon", MaHoaDon.ToString()));

            return ExecuteNonQuery(_cmd);
        }

        //Lay ma hoa don vua them
        public int LayMaHoaDonMoi()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select max(MaHoaDon) from HoaDon";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            String str = this.ExecuteScalar(_cmd);

            if (str == null || str == "")
            {
                return 0;
            }

            return int.Parse(str);
        }




    }
}
